package cn.wolfcode.mq;


import cn.wolfcode.ws.WebSocketServer;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@RocketMQMessageListener(
        consumerGroup = "MQOderResultListener",
        topic = MQConstant.ORDER_RESULT_TOPIC
)
@Component
@Slf4j
public class MQOderResultListener implements RocketMQListener<OrderMQResult> {
    @Override
    public void onMessage(OrderMQResult orderMQResult) {
        System.out.println("--------");
        //先进行睡眠一秒钟
        try {
            Thread.sleep(1000);
        }catch (Exception e){
            e.printStackTrace();
        }
        //怎么把数据返回给客户端
        //通过socket获取到的数据进行解析读取
        WebSocketServer webSocketServer = WebSocketServer.clients.get(orderMQResult.getToken());
        if (webSocketServer != null) {
            try {
                webSocketServer.getSession().getBasicRemote().sendText(JSON.toJSONString(orderMQResult));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
